package com.task.filter;

import com.task.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 账号验证
 *
 * @author jiafujun
 * 2020年01月08日 17:48:00
 */
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("user") == null) {
            response.sendRedirect(request.getContextPath() + "/page/login");
            return false;
        }
        User user = (User) session.getAttribute("user");
        if (user.getId() == null){
            response.sendRedirect(request.getContextPath() + "/page/login");
            return false;
        }
        return true;
    }
}
